import { _decorator, Component, Enum, EventTouch, Label, Node } from 'cc';
import { ReadyState, SeatType, UserInfo } from '../proto/proto';
import { KugoModel } from '../KugoModel';
import { ComponentBase } from '../base/ComponentBase';
import { kugoControl } from '../KugoControl';
const { ccclass, property } = _decorator;

@ccclass('UserComponent')
export class UserComponent extends ComponentBase {
    @property(Node)
    headSpr:Node = null

    @property(Node)
    nameLab:Node = null

    @property(Node)
    scoreLab:Node = null

    @property(Node)
    readyNode:Node = null

    @property(Node)
    readyBtn:Node = null

    
    @property(Node)
    inviteBtn:Node = null

    @property(Node)
    cancelReadyBtn:Node = null
    
    @property(Node)
    timeNode:Node = null

    @property({ type: Enum(SeatType) })
    seatId:SeatType = SeatType.BLACK

    private _model:KugoModel = null
    onLoad() {
        this.readyBtn.active = false
        this.cancelReadyBtn.active = false
        this.headSpr.active = false
        this.readyNode.active = false

        this._model = KugoModel.getInstance()
        if (this.inviteBtn) {
            this.node.children.forEach(node=>{
                if (node != this.inviteBtn) {
                    node.active = false
                }
            })
        }
    }

    onClickBtn(event:EventTouch, evenData:any){
        super.onClickBtn(event, evenData)
        switch (event.target.name) {
            case "BtnUserReady":
                console.log("点击准备")
                kugoControl.c2sUserReady()
                break;
            case "BtnCancelUserReady":
                console.log("点击取消准备")
                break;
            case "BtnInvite":
                console.log("点击邀请")
                break;
            default:
                break;
        }
    }
    private _data:UserInfo = null

    setData(data:UserInfo){
        this._data = data
        
        this.headSpr.active = true
        if (this.inviteBtn) {
            this.inviteBtn.active = false
            this.node.children.forEach(node=>{
                if (node != this.inviteBtn) {
                    node.active = true
                }
            })
        }
       
        this.refresh()
    }

    getSeatId(){
        return this._data.SeatId
    }

    getUserId(){
        return this._data.Data.UserId
    }

    
    refresh() {
        this.nameLab.getComponent(Label).string = this._data.Data.NickName
        if (this._data.Data.UserId == this._model.UserMgr.selfId) {
            if (this._data.ReadyState != ReadyState.READY) {
                this.readyBtn.active = true
                this.cancelReadyBtn.active = false
            }else{
                this.readyBtn.active = false
            }
        }else{
            this.readyBtn.active = false
            this.cancelReadyBtn.active = false
        }
        this.readyNode.active = this._data.ReadyState == ReadyState.READY
        if(this.readyNode.active){
            this.readyBtn.active = false
            this.cancelReadyBtn.active = false
        }
        this.scoreLab.getComponent(Label).string = this._data.Data.Score.toString()
    }    
}


